import {defineStore} from "pinia";
import type {reportType} from "./type";
import {
    ListByReportParams,
    rpDataItemTableDataParams,
    rpDirDataType,
    rpResultData,
    rpResultParams, structResultItem
} from "@/api/reportCom/type";
import {
    getDataItemTableData,
    getDataItemTableHeaders,
    getRpAndDirList,
    getRpResult,
    getRpTime, saveCustomerText,
} from "@/api/reportCom";
import {ElMessage, ElMessageBox} from "element-plus";
import {list} from "postcss";

let useReportStore = defineStore('report', {
    state: (): reportType => {
        return {
            reportLeftItem: {},
            reportTime: '',
            reportTitle: '',
            reportRightItem: {} as structResultItem,
            menuList: [],
            menuRight: {} as rpResultData,
            timeList: [],
            diguiOver: '',
            allTable: [],
            tableLoading: false,
            defaultRightOpends: [],
            defaultLeftOpends: [],
            lookDetailTbaleHeaders: [],
            lookDetailTbaleData: [],
            page: 1,
            limit: 50,
            total: 0,
            dataId: '',
            tableDeatilLoading: false,
        }
    },
    actions: {
        setReportLeftItem(item: rpDirDataType) {
            this.reportLeftItem = item
        },

        setReportRightItem(item: structResultItem) {
            this.reportRightItem = item
        },

        setReportTime(time: string) {
            this.reportTime = time
        },

        async useGetRpAndDirList() {
            let res = await getRpAndDirList()
            if (res.code === 0) {
                this.menuList = res.data
                this.diguiOver = ''
                this.findReportId(this.menuList, 'type', 'report', 'item')
                // console.log(this.findReportId(this.menuList, 'type', 'report', 'item'))
                this.setReportLeftItem(this.diguiOver)
                this.setDefaultLeftOpends(this.menuList)
            } else {
                ElMessage({
                    message: res.msg,
                    type: 'error',
                })
            }

        },

        async useGetRptime() {
            let res = await getRpTime(this.reportLeftItem.id)
            if (res.code === 0) {
                this.timeList = res.data
                this.setReportTime(res.data[res.data.length - 1])
            } else {
                ElMessage({
                    message: res.msg,
                    type: 'error',
                })
            }

        },

        async useGetRpResult() {
            this.tableLoading = true
            let params: rpResultParams
            params = {
                reportId: this.reportLeftItem.id,
                reportTime: this.reportTime
            }
            let res = await getRpResult(params)
            if (res.code === 0) {
                this.menuRight = res.data
                this.menuRight.structResultList = this.setDigui(res.data.structResultList)
                this.diguiOver = ''
                this.findReportId(this.menuRight.structResultList, 'type', 2, 'item')
                this.setReportRightItem(this.diguiOver)
                this.setDefaultRightOpends()
                this.allTable = []
                this.findAllTable(this.menuRight.structResultList)
            } else {
                ElMessage({
                    message: res.msg,
                    type: 'error',
                })
            }
            this.tableLoading = false

        },

        async useGetDataItemTableData(dataId: string) {
            this.tableDeatilLoading = true
            this.dataId = dataId
            let params: rpDataItemTableDataParams
            params = {
                dataId: dataId,
                page: this.page,
                limit: this.limit
            }
            let res = await getDataItemTableData(params)
            if (res.code === 0) {
                this.lookDetailTbaleData = res.data.list
                this.total = res.data.total
            } else {
                ElMessage({
                    message: res.msg,
                    type: 'error',
                })
            }
            this.tableDeatilLoading = false
        },

        async useGetDataItemTableHeaders(dataId: string) {
            let params: { dataId: string }
            params = {dataId: dataId}
            let res = await getDataItemTableHeaders(params)
            if (res.code === 0) {
                this.lookDetailTbaleHeaders = res.data
            } else {
                ElMessage({
                    message: res.msg,
                    type: 'error',
                })
            }
        },

        async useGetDataItemTableHeadsers(dataId: string) {
            let params: { dataId: string }
            params = {dataId: dataId}
            let res = await getDataItemTableHeaders(params)
            if (res.code === 0) {
                this.lookDetailTbaleHeaders = res.data
            } else {
                ElMessage({
                    message: res.msg,
                    type: 'error',
                })
            }
        },
        async useSaveCustomerText() {
            this.tableLoading = true
            let params = {
                instanceStructId: this.reportRightItem.id,
                text: this.reportRightItem.customerText
            } as { instanceStructId: string | number, text: string }
            try {
                let res = await saveCustomerText(params)
                ElMessage({
                    message: '保存成功',
                    type: 'success',
                })
                this.tableLoading = false
            } catch (err) {
                this.tableLoading = false
            }
        },

        //@ts-ignore
        findReportId(list: any, key: string, value: string | number, returnName: string) {
            for (let i = 0; i < list.length; i++) {
                if (list[i][key] == value) {
                    if (this.diguiOver != '') {
                        return this.diguiOver
                    } else {
                        if (returnName == 'item') {
                            this.diguiOver = list[i]
                        } else {
                            this.diguiOver = list[i][returnName]
                        }
                        break;
                    }
                } else {
                    return this.findReportId(list[i].children, key, value, returnName)

                }
            }
        },

        setDigui(list: any[]) {
            for (let i = 0; i < list.length; i++) {
                list[i].id = list[i].id + ''
                if (list[i].children && list[i].children.length > 0) {
                    this.setDigui(list[i].children)
                }
            }
            return list
        },

        findAllTable(list: any[]) {
            for (let i = 0; i < list.length; i++) {
                if (list[i].type == 2) {
                    this.allTable.push(
                        list[i]
                    )
                } else {
                    if (list[i].children && list[i].children.length > 0) {
                        this.findAllTable(list[i].children)
                    }
                }
            }
        },

        findReportTitleItem(list: any[]) {

        },

        setDefaultRightOpends() {
            this.defaultRightOpends = []
            this.menuRight.structResultList.forEach((item) => {
                let id = item.id
                this.defaultRightOpends.push(id)
            })
            console.log(this.defaultRightOpends)
        },

        setDefaultLeftOpends(list: any[]) {
            for (let i = 0; i < list.length; i++) {
                if (list[i].children) {
                    this.defaultLeftOpends.push(list[i].id)
                    this.setDefaultLeftOpends(list[i].children)
                }
            }

        },

        changePage(page: number, fn: void) {
            this.page = page
            this.useGetDataItemTableData(this.dataId)
        },

        changeLimit(limit: number, fn: void) {
            this.limit = limit
            this.useGetDataItemTableData(this.dataId)
        },

    },
    getters: {}
})

export default useReportStore
